
$(document).ready($(function() {
	
	$.validator.addMethod("emailRegex", function(value, element) {
		return this.optional(element) || /^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]+$/i.test(value);
	});
	$.validator.addMethod("loginRegex", function(value, element) {
		return this.optional(element) || /^([a-zA-Z\u0400-\u052F0-9]+[-_]?)*$/i.test(value);
	});
	$.validator.addMethod("passwordRegex", function(value, element) {
		return this.optional(element) || /^[a-zA-Z\u0400-\u052F0-9_-]+$/i.test(value);
	});
	$.validator.addMethod("firstNameRegex", function(value, element) {
		return this.optional(element) || /^([a-zA-Z\u0400-\u052F]+[-// /']?)*$/i.test(value);
	});
	$.validator.addMethod("lastNameRegex", function(value, element) {
		return this.optional(element) || /^([a-zA-Z\u0400-\u052F]+[-// /']?)*$/i.test(value);
	});
	$("#account-data-form").validate({
		onkeyup : function(element, event) {
			if (event.which === 9) {
				return;
			} else if ($(this).val(element) === "") {

			} else {
				this.element(element);
			}
		},
		rules : {
			inputLogin : {
				required : true,
				minlength : 4,
				maxlength : 25,
				loginRegex : true
			},
			inputEmail : {
				required : true,
				emailRegex : true
			},
			inputFName : {
				minlength : 2,
				maxlength : 30,
				firstNameRegex : true
			},
			inputLName : {
				minlength : 2,
				maxlength : 30,
				lastNameRegex : true
			},
			inputPassword : {
				required : true,
				minlength : 8,
				maxlength : 16,
				passwordRegex : true
			},
			inputConfirmPassword : {
				required : true,
				minlength : 8,
				maxlength : 16,
				passwordRegex : true,
				equalTo : "#inputPassword"
			},
			sex : {
				required : true
			}
		},
		messages : {
			inputLogin : {
				required : "Please enter your login.",
				minlength : "The login must be at least 4 characters long.",
				maxlength : "The login must be at least than 25 characters long.",
				loginRegex : "The login must contain only letters, numbers, dashes or hyphens."
			},
			inputEmail : {
				required : "Please enter your email.",
				emailRegex : "Please enter a valid email address."
			},
			inputPassword : {
				required : "Please enter your password",
				minlength : "The password must be more than 8 characters long",
				maxlength : "The password name must be less than 16 characters long.",
				passwordRegex : "The password must contain only letters, numbers, dashes or hyphens."
			},
			inputConfirmPassword : {
				required : "Please repeat your password",
				minlength : "The password must be more than 8 characters long.",
				maxlength : "The password name must be less than 16 characters long.",
				passwordRegex : "The password must contain only letters, numbers, dashes or hyphens.",
				equalTo : "Please enter the same password."
			},
			inputFName : {
				minlength : "The first name must be more than 2 characters long.",
				maxlength : "The first name must name must be less than 30 characters long.",
				firstNameRegex : "The first name must contain only contain only letters,apostrophe or hyphen."
			},
			inputLName : {
				minlength : "The last name must be more than 2 characters long.",
				maxlength : "The last name must be name must be less than 30 characters long.",
				lastNameRegex : "The last name must contain only letters,apostrophe or hyphen."
			},
			sex : {
				required : "Please select sex"
			}
		},
		highlight : function(element) {
			$(element).closest('.form-group').removeClass('has-success has-feedback').addClass('has-error has-feedback');
		},
		success : function(element) {
			$(element).closest('.form-group').removeClass('has-error has-feedback').addClass('has-success has-feedback');
		},
		submitHandler : function(form) {
			form.submit();
		}
	});
}));
	
	
$(document).ready(function() {
	$("#inputLogin").on('focusout submit', function() {
		var $label = $('label[for="inputLogin"][class="error"]');
		if (!!$label.length == false || $label.text().length == 0) {
			var inputLogin = $(this).val();
			$(".statusLogin").html("Checking availability...");
			$.ajax({
				type : "POST",
				url : "checkAvailability",
				data : "inputLogin=" + inputLogin,
				success : function(msg) {
					$(".statusLogin").html("");
					var $test = msg.trim();
					if ($test == '1') {
						$(".statusLogin").removeClass("glyphicon glyphicon-remove form-control-feedback");
						$(".statusLogin").parent().parent().removeClass("has-error has-feedback");
						$(".statusLogin").addClass("glyphicon glyphicon-ok form-control-feedback");
						$(".statusLogin").parent().parent().addClass("has-success has-feedback");
					} else {
						$(".statusLogin").removeClass("glyphicon glyphicon-ok form-control-feedback");
						$(".statusLogin").parent().parent().removeClass("has-success has-feedback");
						$(".statusLogin").addClass("glyphicon glyphicon-remove form-control-feedback");
						$(".statusLogin").parent().parent().addClass("has-error has-feedback");
						$label.text("Not available.");
					}
				}
			});
		} else {
			$(".statusLogin").removeClass("glyphicon glyphicon-ok form-control-feedback");
			$(".statusLogin").parent().parent().removeClass("has-success has-feedback");
			$(".statusLogin").addClass("glyphicon glyphicon-remove form-control-feedback");
			$(".statusLogin").parent().parent().addClass("has-error has-feedback");
		}
	});
});
$(document).ready(function() {
	$("#inputEmail").on('focusout submit', function() {
		var $label = $('label[for="inputEmail"][class="error"]');
		if (!!$label.length == false || $label.text().length == 0) {
			var inputEmail = $(this).val();
			$(".statusEmail").html("Checking availability...");
			$.ajax({
				type : "POST",
				url : "checkAvailability",
				data : "inputEmail=" + inputEmail,
				success : function(msg) {
					$(".statusEmail").html("");
					var $test = msg.trim();
					if ($test == '1') {
						$(".statusEmail").removeClass("glyphicon glyphicon-remove form-control-feedback");
						$(".statusEmail").parent().parent().removeClass("has-error has-feedback");
						$(".statusEmail").addClass("glyphicon glyphicon-ok form-control-feedback");
						$(".statusEmail").parent().parent().addClass("has-success has-feedback");
					} else {
						$(".statusEmail").removeClass("glyphicon glyphicon-ok form-control-feedback");
						$(".statusEmail").parent().parent().removeClass("has-success has-feedback");
						$(".statusEmail").addClass("glyphicon glyphicon-remove form-control-feedback");
						$(".statusEmail").parent().parent().addClass("has-error has-feedback");
						$label.text("Not available.");
					}
				}
			});
		} else {
			$(".statusEmail").removeClass("glyphicon glyphicon-ok form-control-feedback");
			$(".statusEmail").parent().parent().removeClass("has-success has-feedback");
			$(".statusEmail").addClass("glyphicon glyphicon-remove form-control-feedback");
			$(".statusEmail").parent().parent().addClass("has-error has-feedback");
		}
	});
});




